package com.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.order.bean.User;
import com.order.common.entiry.ResultCode;
import com.order.common.entiry.RoleEnum;
import com.order.common.exception.CommonException;
import com.order.common.util.SqlSessionFactoryUtil;
import com.order.mapper.UserMapper;
import com.order.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    private SqlSession sqlSession;

    private void closeMapper() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    private void initMapper() {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();
        SqlSession sqlSession = factory.openSession(true);
        userMapper = sqlSession.getMapper(UserMapper.class);
    }

    @Override
    public User login(User user) {
        initMapper();
        String username = user.getUsername();
        User dbUser = userMapper.selectByUsername(username);
        if (dbUser == null) {
            new CommonException(ResultCode.ACCOUNT_NOT_FOUND);
        } else if (!dbUser.getPassword().equals(dbUser.getPassword())) {
            new CommonException(ResultCode.UNMOBILE_OR_PASSWORD);
        }
        closeMapper();
        return dbUser;
    }

    @Override
    public boolean addUser(User user) {
        initMapper();
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser != null) {
            new CommonException(ResultCode.ACCOUNT_ALREADY_EXISTS);
        } else if (user.getPassword() == null) {
            new CommonException(ResultCode.PASSWORD_CANNOT_BE_EMPTY);
        }
        if (user.getUsername() == null) {
            user.setName(user.getUsername());
        }
        user.setRole(RoleEnum.USER.name());
        boolean ins = userMapper.insert(user);
        closeMapper();
        return ins;
    }

    @Override
    public boolean updateUser(User user) {
        initMapper();
        boolean up = userMapper.updateById(user);
        closeMapper();
        return up;
    }

    @Override
    public boolean deleteById(Integer id) {
        initMapper();
        boolean del = userMapper.deleteById(id);
        closeMapper();
        return del;
    }

    @Override
    public boolean deleteBatch(Integer[] ids) {
        for (Integer id :ids){
            boolean b = this.deleteById(id);
            if (!b) return b;
        }
        return true;
    }

    @Override
    public User selectUserById(Integer id) {
        initMapper();
        User dbUser = userMapper.selectById(id);
        closeMapper();
        return dbUser;
    }

    @Override
    public List<User> selectAll(String name) {
        initMapper();
        List<User> dbusers = userMapper.selectAll(name);
        closeMapper();
        return dbusers;
    }

    @Override
    public PageInfo<User> selectPage(String name, Integer pageNum, Integer pageSize) {
        initMapper();
        PageHelper.startPage(pageNum, pageSize);
        List<User> dbusers = this.selectAll(name);
        closeMapper();
        return PageInfo.of(dbusers);
    }
}
